package br.com.regressao.linear.controller;

import java.awt.Color;

import org.apache.commons.math3.stat.regression.SimpleRegression;
import org.math.plot.Plot2DPanel;
import org.math.plot.plotObjects.BaseLabel;

/**
 *
 * @author Edidelson
 */
public class LinearRegression {

    private final SimpleRegression sr = new SimpleRegression();
    private final Plot2DPanel plot = new Plot2DPanel();

    public LinearRegression(double[] x, double[] y) {
        for (int i = 0; i < x.length; i++) {
            sr.addData(x[i], y[i]);
        }
        double[] yc = new double[y.length];
        for (int i = 0; i < x.length; i++) {
            yc[i] = sr.predict(x[i]);
        }
        plot.addLegend("South");
        plot.addScatterPlot("Dados", x, y);
        plot.addLinePlot("Regressão", x, yc);

        BaseLabel titulo = new BaseLabel("GRÁFICO DE DISPERSÃO", Color.GREEN,
                0.5, 1.1);
        plot.addPlotable(titulo);
    }
}
